package com.imay.live.activity.family.manager;

import android.text.TextUtils;

import com.imay.live.helper.EventHelper;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.tbas_api.IMYTBASObject;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;


/**
 * Created by Administrator on 2017/9/14 0014.
 */

public class FamilyUserChangeManager {

    public static final String ACTION_CHANGE_ELDER = "action_change_elder"; // 家族转移
    public static final String ACTION_USER_REFREST = "action_user_refrest"; // 家族转移

    private IMYTBASObject familyUserNotify;

    public static class Builder {

        private final FamilyUserChangeListener listener;

        public static FamilyUserChangeManager.Builder create(FamilyUserChangeListener listener) {
            if (listener == null) {
                throw new NullPointerException("listener == null");
            }
            return new FamilyUserChangeManager.Builder(listener);
        }

        protected Builder(FamilyUserChangeListener listener) {
            this.listener = listener;
        }

        public FamilyUserChangeManager build() {
            FamilyUserChangeManager familyUserChangeManager = new FamilyUserChangeManager();
            familyUserChangeManager.registerFamilyNotify(listener);
            return familyUserChangeManager;
        }
    }

    /**
     * 加载数据
     */
    public static void elderChange() {
        EventHelper.postFamilyUserChange(ACTION_CHANGE_ELDER);
    }

    public static void userRefresh() {
        EventHelper.postFamilyUserChange(ACTION_USER_REFREST);
    }

    public interface FamilyUserChangeListener {
        void elderChange(); // 族长更换

        void userRefresh(); // 成员更新
    }

    /**
     * 用户失效的通知
     */
    public void registerFamilyNotify(final FamilyUserChangeListener listener) {
        familyUserNotify = new IMYTBASObject(false);
        familyUserNotify.stringCommand = EventHelper.EVENT_FAMILY_USER_CHANGE;
        familyUserNotify.setDataResultListener(new IMYTBASObject.OnDataResultListener() {
            @Override
            public void onCommandTaskSuccess(IMYTBASObject object) {
                if (ACTION_CHANGE_ELDER.equals(object.stringData)) {
                    listener.elderChange();
                } else if (ACTION_USER_REFREST.equals(object.stringData)) {
                    listener.userRefresh();
                }
            }
        });
        familyUserNotify.addEventListenWithTBASObject();
    }

    public void unregisterFamilyNotify() {
        if (familyUserNotify != null) {
            familyUserNotify.destroy();
        }
    }
}
